﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.Common;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Models.Category;
using X.PagedList;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    public class CategoryController : Controller
    {
        private ICategoryService _icgSvc;
        public CategoryController(ICategoryService icgSvc)
        {
            _icgSvc = icgSvc;
        }

        public async Task<IActionResult> Index(string search, int page = 1)
        {
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _icgSvc.GetByNameAsync(search);
            var dataList = new List<CategoryListViewModel>();
            foreach (var item in data)
            { 
                var CgList = new CategoryListViewModel
                {
                    Id = item.Id,
                    Title = item.Title,
                    ParentTitle = await GetCategoryParentTitle(item.ParentId),
                    UpdateTime = item.UpdateTime
                };
                dataList.Add(CgList);
            }
            ViewBag.Search = search;
            var pages = await dataList.ToPagedListAsync(page,PageConfig.PageSize);
            return View(pages);
        }

        private async Task<string> GetCategoryParentTitle(int parentId) 
        {
            if (parentId == 0)
                return "顶级菜单";
            var data = await _icgSvc.GetByIdAsync(parentId);
            return data.Title;
        }

        [HttpGet]
        public async Task<IActionResult> Update(int id) 
        {
            var data = await _icgSvc.GetByIdAsync(id);
            var list = new List<int>();
            if (data.ParentId != 0)
            {
                var parentId = await _icgSvc.GetByIdAsync(data.Id);
                if (parentId.ParentId == 0)
                    list.AddRange(new int[] { 2, data.ParentId, 0 });
            }
            else 
            {
                list.AddRange(new int[] { 1, 0, 0 });
            }
            return View(new CategoryUpdateViewModel 
            {
                Id = data.Id,
                Title = data.Title,
                ParentList = list
            });
        }

        [HttpPost]
        public async Task<IActionResult> Update(CategoryUpdateViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                int parentId = 0;
                if (model.ParentList[0] == 2)
                    parentId = model.ParentList[1];
                var res = await _icgSvc.UpdateCategoryAsync(model.Id,model.Title,parentId);
                if (res > 0)
                    return Redirect("~/ForumManage/Category/Index");
            }
            return View(model);
        }

        [HttpGet]
        public async Task<IActionResult> GetCategoryList(int parentId = 0) 
        {
            var data = await _icgSvc.GetByParentIdAsync(parentId);
            var dataList = data.Select(cg => new CategoryDropdownViewModel
            {
                Id = cg.Id,
                Title = cg.Title
            }).ToList();
            return Json(dataList);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id) 
        {
            if (ModelState.IsValid) 
            {
                await _icgSvc.PutTrashAsync(id);
                return Redirect("~/ForumManage/Category/Index");
            }
            return View();
        }

    }
}
